
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;

public class SpriteStore {

	private static SpriteStore single = new SpriteStore();
	
	public static SpriteStore get(){
		return single;
	}
	private HashMap<String, Sprite> sprites= new HashMap<String, Sprite>();


	public Sprite getSprite(String spr){
		if(sprites.get(spr)!=null)
		{
			return (Sprite) sprites.get(spr);
		}
		
		BufferedImage sourceImage = null;
		
		
		try{
			
			URL url =this.getClass().getClassLoader().getResource(spr);
			if(url ==null)
			{
				fail("Can't find spr: "+spr);
			}
			sourceImage =ImageIO.read(url);
		}catch(IOException e){
			fail("Failed to load: "+spr);
		}
		GraphicsConfiguration gc =GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
		
	
		image.getGraphics().drawImage(sourceImage,0,0,null);
		
	
		Sprite sprite = new Sprite(image);
		sprites.put(spr,sprite);
		
		return sprite;
	
	}


	private void fail(String message){
		System.err.println(message	);
		System.exit(0);
	}

}
